//
//  ParentFoucsViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/3/30.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class ParentFoucsViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView!
    var label: UILabel!
    
    var classArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "家长关注"
        
        InitViews()
        setUpRefresh()
//        getCount()
    }
    
    func InitViews() {
        let header: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH/3))
        header.backgroundColor = barColor
        
        label = UILabel.init(frame: CGRect(x: (screenW - 150)/2, y: (screenH/3 - 150)/2, width: 150, height: 150))
        label.layer.masksToBounds = true
        label.layer.borderColor = UIColor.white.cgColor
        label.layer.borderWidth = 3.0
        label.layer.cornerRadius = 75
        label.numberOfLines = 0
        label.textColor = UIColor.white
        label.backgroundColor = barColor
        label.textAlignment = NSTextAlignment.center
        header.addSubview(label)
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        tableView.tableHeaderView = header
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        self.view.addSubview(tableView)
    }
    
    func setUpRefresh() {
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(ParentFoucsViewController.getCount))
        self.tableView.header.beginRefreshing()
    }
    /**
     获取数据
     */
    func getCount() {
        
        if (reachability?.isReachable())! {
            
            let user = getUserPojo()
            let requestxml = requestHeader + "4</t><sch>" + user.schid + "</sch></channel></rss>"
            let url = server + adminwebapppi
            let result = HttpUtils.httpPostUTF(url, with: requestxml)
            print(result!)
            if result != nil {
                
                //1228 - 本地化
                //++++++++++++++++++++++++++++++++++++++++++
                let array = JSON(data: (result?.data(using: String.Encoding.utf8))!)["post"]
                
                if array.count != 0 {
                    classArray = NSMutableArray()
                    for i in 0..<array.count {
                        let dic = array[i]
                        
                        let count = dic["FamFocusCon"].stringValue
                        let classArray1 = dic["ClassList"]
                        classArray.addObjects(from: classArray1.arrayValue)
                        
                        let PV = count + "\n家长关注／人"
                        let pv = NSMutableAttributedString.init(string: PV)
                        pv.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 45), range: NSMakeRange(0, count.characters.count))
                        pv.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(count.characters.count, pv.length - count.characters.count))
                        label.attributedText = pv
                        
                    }
                }
            }
            self.tableView.reloadData()
            
        } else {
            HYLog("网络连接：不可用")
        }
        
        self.tableView.header.endRefreshing()
        
    }
    
    //MARK: - table view delegate and data sourrce 
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return classArray.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "ClassCountTableViewCellIdentifier";
        
        let cell: ClassCountTableViewCell = ClassCountTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        let dic = classArray[indexPath.row] as! JSON
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
//        let classid = dic.objectForKey("classid") as! String//班级id
        let strper = dic["strper"].stringValue//百分比
        let classname = dic["classname"].stringValue//班级名称
        let foucs = dic["famfoucs"].stringValue//关注数
        cell.classname.text = classname
        cell.foucs.text = "关注:\(foucs)人"
        cell.count.text = strper + "%"
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let dic = classArray[indexPath.row] as! JSON
        let classid = dic["classid"].stringValue//班级id
        let listVC = ParentFoucsListViewController()
        listVC.classid = classid
        self.navigationController?.pushViewController(listVC, animated: true)
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
